package component.global.utils
{
	import component.controls.interfaces.IDispose;
	import component.controls.ui.Component;
	import component.global.manager.EventMgr;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.EventDispatcher;
	import flash.utils.describeType;

	public class ObjectUtil
	{
		public static function dispose(obj:*):void{
			if(obj == null)return;
			if(obj is EventDispatcher)EventMgr.Instance.removeEvent(obj);
			if(obj is IDispose){
				obj.dispose();
			} else if(obj is MovieClip){
				obj.stop();
			} else if(obj is DisplayObjectContainer){
				DisplayUtils.removeAllChildren(obj);
			} else if(obj is Shape){
				obj.graphics.clear();
			} 
			if(obj is DisplayObject){
				DisplayUtils.removeDisplay(obj as DisplayObject);
			}
			if(obj is Bitmap){
				(obj as Bitmap).bitmapData.dispose();
			}
			if(obj is BitmapData){
				(obj as BitmapData).dispose();
			}
			if(obj.hasOwnProperty("filters")){
				obj.filters = null;
			}
			obj = null;
		}
		
		public static function disposeElements(elements:*):void{
			if(elements == null)return;
			while(elements.length>0){
				dispose(elements.shift());
			}
		}
		
		/**
		 * 将info上的属性复制到 item上
		 * @param item
		 * @param info
		 * 
		 * 
		 */		
		public static function copyProperties(item:*,info:*):void{
			var xml:XML=describeType(info);
			var xmlList:XMLList = xml.variable;
			for (var i:int = 0; i < xmlList.length(); i++) 
			{
				var pro:String = String(xmlList[i].@name);
				if(item.hasOwnProperty(pro)){
					item[pro] = info[pro];
				}
			}
		}
		
		/**
		 * 
		 * 通过XML对目标对象进行赋值
		 * @param object: 赋值的目标对象
		 * @param data : 保存属性的XML对象
		 * 
		 */		
		public static function copyPorpertiesByXML(object:Object,data:XML):void
		{
			var attr:XMLList =data.attributes();
			for each(var item:XML in attr)
			{
				var propname:String = item.name().toString();
				if(object.hasOwnProperty(propname))
				{
					try{
						var value:String = item.toString();
						if(value == "false")
						{
							object[propname] = false;
						}else
						{
							object[propname] = value;
						}
					}catch(e:Error)
					{
						trace("错误:",e.message);
					}
				}
			}
		}
	}
}